package element.ast;

import intermediate_representation.ExpRP;
import table.InterCodeTable;

public class DopNode extends AstNode {
    private String op;
    private AstNode leftNode;
    private AstNode rightNode;

    public DopNode(String op, AstNode leftNode, AstNode rightNode) {
        this.op = op;
        this.leftNode = leftNode;
        this.rightNode = rightNode;
    }

    @Override
    public String tointerRP() {
        int nowindex = InterCodeTable.regindex;
        String left = leftNode.tointerRP();
        String right = rightNode.tointerRP();
        InterCodeTable.regindex = nowindex;
//        if (left.charAt(0) == '$' && right.charAt(0) == '$') {
//            int leftr = Integer.parseInt(left.substring(4));
//            int rightr = Integer.parseInt(right.substring(4));
//            if (leftr < rightr) {
//                InterCodeTable.regindex = leftr;
//            } else {
//                InterCodeTable.regindex = rightr;
//            }
//        } else if (left.charAt(0) == '$') {
//            int leftr = Integer.parseInt(left.substring(4));
//            InterCodeTable.regindex = leftr;
//        } else if (right.charAt(0) == '$') {
//            int rightr = Integer.parseInt(right.substring(4));
//            InterCodeTable.regindex = rightr;
//        }
        InterCodeTable.pushvar();
        ExpRP expRP = new ExpRP(left, right, op, InterCodeTable.nowVar);
        InterCodeTable.addCode(expRP);
        return InterCodeTable.nowVar;
    }

    @Override
    public void tofuncRP() {
        leftNode.tofuncRP();
        rightNode.tofuncRP();
    }
}
